package com.xiaosheng.ljswl;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;
import com.xiaosheng.ljswl.fragment.ColorFragment;
import com.xiaosheng.ljswl.fragment.DianZiChengFragment;
import com.xiaosheng.ljswl.fragment.GuangXianFragment;
import com.xiaosheng.ljswl.fragment.HongWaiCeJuFragment;
import com.xiaosheng.ljswl.fragment.HongWaiDuiSheFragment;
import com.xiaosheng.ljswl.fragment.HuoYanFragment;
import com.xiaosheng.ljswl.fragment.KeRanFragment;
import com.xiaosheng.ljswl.fragment.MultFragment;
import com.xiaosheng.ljswl.fragment.QiYaFragment;
import com.xiaosheng.ljswl.fragment.ShuiWeiFragment;
import com.xiaosheng.ljswl.fragment.YangQiFragment;
import com.xiaosheng.ljswl.fragment.YaoKongFragment;
import com.xiaosheng.ljswl.fragment.RelayFragment;
import com.xiaosheng.ljswl.fragment.SpeedUpFragment;
import com.xiaosheng.ljswl.fragment.WenShiDuFragment;
import com.xiaosheng.ljswl.fragment.YuDiFragment;
import com.xiaosheng.ljswl.fragment.ZhenDongFragment;

public class MainActivity extends AppCompatActivity {


    private DrawerLayout body_box;
    private NavigationView cehua;
    private Toolbar toolbar;
    private Button nav_menu;
    private TextView bar_context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        body_box = findViewById(R.id.body_box);
        cehua = findViewById(R.id.cehua);
        toolbar = findViewById(R.id.app_bar);
        nav_menu = findViewById(R.id.nav_menu);
        bar_context = findViewById(R.id.bar_context);
        nav_menu.setOnClickListener(view -> {
            if (body_box.isDrawerOpen(Gravity.LEFT))
                body_box.closeDrawers();
            else
                body_box.openDrawer(Gravity.LEFT);
        });
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        FragmentManager manager = getSupportFragmentManager();
        cehua.setItemIconTintList(null);
        cehua.setNavigationItemSelectedListener(item -> {
            Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
            Class target = null;
            switch (item.getItemId()) {
                case R.id.wenshidu:
                    target = WenShiDuFragment.class;
                    break;
                case R.id.guangxian:
                    target = GuangXianFragment.class;
                    break;
                case R.id.jidianqi:
                    target = RelayFragment.class;
                    break;
                case R.id.duogongneng:
                    target = MultFragment.class;
                    break;
                case R.id.yudi:
                    target = YuDiFragment.class;
                    break;
                case R.id.speedup:
                    target = SpeedUpFragment.class;
                    break;
                case R.id.color:
                    target = ColorFragment.class;
                    break;
                case R.id.qiya:
                    target = QiYaFragment.class;
                    break;
                case R.id.yaokong:
                    target = YaoKongFragment.class;
                    break;
                case R.id.keran:
                    target = KeRanFragment.class;
                    break;
                case R.id.huoyan:
                    target = HuoYanFragment.class;
                    break;
                case R.id.dianzicheng:
                    target = DianZiChengFragment.class;
                    break;
                case R.id.zhendong:
                    target = ZhenDongFragment.class;
                    break;
                case R.id.hongwaiduishe:
                    target = HongWaiDuiSheFragment.class;
                    break;
                case R.id.shuiwei:
                    target = ShuiWeiFragment.class;
                    break;
                case R.id.hongwaiceju:
                    target = HongWaiCeJuFragment.class;
                    break;
                case R.id.yangqi:
                    target = YangQiFragment.class;
                    break;
            }
            try {
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.tv_content, (Fragment) target.newInstance()).commit();
                bar_context.setText(item.getTitle());
                body_box.closeDrawers();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            return false;
        });
    }

    public DrawerLayout getBody_box() {
        return body_box;
    }
}